﻿using System;
using OdsServer.Common;
using OdsServer.Device.Core;
using OdsServer.Common.Enum;

namespace OdsServer.Device
{
    public class DeviceInteropAI : DriverInterop, IDeviceInteropAI
    {
        private PT_AIBinaryIn binaryReadParam;

        public void ReadBinary(ushort channel, out ushort value)
        {
            lock (this)
            {
                if (this.IsReady)
                {
                    this.binaryReadParam.chan = channel;
                    unsafe
                    {
                        fixed (PT_AIBinaryIn* paramAddr = &this.binaryReadParam)
                        {
                            fixed (ushort* valPtr = &value)
                            {
                                this.binaryReadParam.reading = new IntPtr(valPtr);
                            }
                            int result = DeviceDriverAdv.DRV_AIBinaryIn(
                                    this.Handle,
                                    new IntPtr(paramAddr));
                        }
                    }
                }
                else
                    throw new DeviceException("Device not ready");
            }
        }
    }
}